/*
 * This file is part of aKoustiK.

 * aKoustiK is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * aKoustiK is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with aKoustiK.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <KIcon>
#include <KLocale>
#include <KMenu>
#include <QtCore/QDebug>

#include <tray.h>
#include <kore.h>

Tray::Tray(QObject* parent) : QObject(parent), mainWindow(0) {
    qDebug("Tray: creating KStatusNotifier...");
    {
        statusNotifier = new KStatusNotifierItem(this);
        statusNotifier->setIconByName("preferences-desktop-sound");
        statusNotifier->setCategory(KStatusNotifierItem::ApplicationStatus);
        statusNotifier->setStatus(KStatusNotifierItem::Active);

        connect(statusNotifier, SIGNAL(activateRequested(bool,QPoint)), SLOT(statusNotifierActivated()));

        KMenu* menu = new KMenu("aKoustiK");
        menu->addTitle(KIcon("preferences-desktop-sound"), "aKoustiK");

        KActionCollection* actions = Kore::instance().actionCollection();

        QMenu* currentItemMenu = menu->addMenu(i18n("Other"));
        currentItemMenu->addAction(actions->action("remove-current"));

        menu->addSeparator();
        menu->addAction(actions->action("mute"));
        menu->addSeparator();
        menu->addAction(actions->action("previous"));
        menu->addAction(actions->action("play"));
        menu->addAction(actions->action("pause"));
        menu->addAction(actions->action("stop"));
        menu->addAction(actions->action("next"));
        // TODO not sure if we need separator here
        //menu->addSeparator();

        statusNotifier->setContextMenu(menu);

        connect(Kore::instance().player(), SIGNAL(currentItemChanged(int,int)), SLOT(currentItemChanged(int,int)));
    }
    qDebug("Tray: KStatusNotifier created.");

    // TODO use overlay icons for different statuses
}

Tray::~Tray() {
    qDebug("Tray: destructor invoked");
}

void Tray::statusNotifierActivated() {
    if (mainWindow == 0) {
        qDebug("Tray: creating MainWindow...");
        mainWindow = new MainWindow();
        connect(mainWindow, SIGNAL(mainWindowClosed()), SLOT(mainWindowClosed()));
        mainWindow->show();
    }
    else {
        qDebug("Tray: closing MainWindow...");
        mainWindow->close();
    }
}

void Tray::mainWindowClosed() {
    qDebug("Tray: deleting existing MainWindow...");
    mainWindow->deleteLater();
    mainWindow = 0;
}

void Tray::currentItemChanged(int newItem, int oldItem) {
    qDebug("Tray: setting tooltip for item %i...", newItem);
    Q_UNUSED(oldItem);
    // newItem can be -1 if there is no current item:
    if (newItem < 0) {
        return;
    }
    Item* item = Kore::instance().playlist()->at(newItem);
    QString string;
    if (item == 0) {
        string += "";
    }
    else {
        const QString& artist = item->artist();
        const QString& album = item->album();
        const QString& year = item->year();
        const QString& time = item->duration();

        string += "<center style=\"text-align:center; white-space:nowrap;\">";
        string += "<b>";
        string += item->title();
        string += "</b>";
        if (!time.isEmpty()) {
            string += " (";
            string += time;
            string += ")";
        }
        if (!artist.isEmpty()) {
            string += "<br/>";
            string += artist;
            if (!album.isEmpty()) {
                string += " - ";
                string += album;
                if (!year.isEmpty()) {
                    string += " (";
                    string += year;
                    string += ")";
                }
            }
        }
        string += "</center>";
    }

    statusNotifier->setToolTip("preferences-desktop-sound", "aKoustiK", string);
    qDebug("Tray: tooltip set.");
}
